package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BQuestion;
import com.ruoyi.system.domain.vo.BQuestionVo;
import com.ruoyi.system.service.BQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 题库管理
 * @author yangxz
 **/
@RestController
@RequestMapping("/business/question")
public class QuestionController extends BaseController {

    @Autowired
    BQuestionService questionService;

    /**
     * 获取题目列表
     */
//    @PreAuthorize("@ss.hasPermi('dict:question:list')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @GetMapping("/list")
    public TableDataInfo list(BQuestion question)
    {
        startPage();
        List<BQuestionVo> list = questionService.selectQuestionList(question);
        return getDataTable(list);
    }

    /**
     * 根据题目Id获取详细信息
     */
//    @PreAuthorize("@ss.hasPermi('dict:question:query')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @GetMapping(value = "/{questionId}")
    public AjaxResult getInfo(@PathVariable Long questionId) {
        BQuestionVo questionVo = questionService.selectQuestionById(questionId);
        return success(questionVo);
    }

    /**
     * 新增题目
     */
//    @PreAuthorize("@ss.hasPermi('dict:question:add')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @Log(title = "新增题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BQuestion question)
    {
        question.setCreateBy(getUsername());
        question.setCreateTime(new Date());
        return toAjax(questionService.save(question));
    }

    /**
     * 修改题目
     */
//    @PreAuthorize("@ss.hasPermi('dict:question:edit')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @Log(title = "修改题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BQuestion question)
    {
        question.setUpdateBy(getUsername());
        question.setUpdateTime(new Date());
        return toAjax(questionService.updateById(question));
    }

    /**
     * 删除题目
     */
//    @PreAuthorize("@ss.hasPermi('dict:question:remove')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @Log(title = "删除题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{questionIds}")
    public AjaxResult remove(@PathVariable Long[] questionIds)
    {
        return toAjax(questionService.deleteQuestionByIds(questionIds));
    }
}
